package com.yqg.manage.controller;


import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yqg.manage.common.interceptor.SysLog;
import com.yqg.manage.entity.Place;
import com.yqg.manage.mapper.PlaceAccountMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.manage.common.Result;

import com.yqg.manage.service.IPlaceAccountService;
import com.yqg.manage.entity.PlaceAccount;

import org.springframework.web.bind.annotation.RestController;

/**
 * PlaceAccountController 前端控制器
 *
 * @author zxf
 * @since 2025-08-26
 */
@RestController
@RequestMapping("/place-account")
public class PlaceAccountController {

    @Resource
    private IPlaceAccountService placeAccountService;

    @Resource
    private PlaceAccountMapper placeAccountMapper;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    @SysLog("新增或者更新企事业单位账号")
    public Result save(@RequestBody PlaceAccount placeAccount) {

        PlaceAccount account = placeAccountService.preStorageProcessing(placeAccount);
        if(account == null){
            // 账户名被占用
            return Result.error("该用户名已被占用");
        } else if (account.getPlaceId() == null) {
            // 公司被其他人绑定
            return Result.error("该信用代码已被账户绑定");
        }else{
            return Result.success(placeAccountService.saveOrUpdate(placeAccount));
        }
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    @SysLog("删除企事业单位账号")
    public Result delete(@PathVariable Integer id) {
        PlaceAccount placeAccount = placeAccountService.getById(id);
        placeAccount.setIsDelete("1");
        return Result.success(placeAccountService.saveOrUpdate(placeAccount));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<PlaceAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        List<PlaceAccount> list = placeAccountService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(placeAccountService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(required = false) String param) {
        pageNum = (pageNum - 1) * pageSize;
        Map<String, Object> map = new HashMap<>();
        map.put("records", placeAccountMapper.getPage(pageNum, pageSize, param));
        map.put("total", placeAccountMapper.getPageCount(param));
        return Result.success(map);
    }


    // 重置密码
    @GetMapping("/resetPassword")
    @SysLog("重置商户账户密码")
    public Result resetPassword(@RequestParam Long id) {
        PlaceAccount placeAccount = new PlaceAccount();
        placeAccount.setId(id);
        placeAccount.setPassword(SecureUtil.md5("123456"));
        return Result.success(placeAccountService.saveOrUpdate(placeAccount));
    }
}

